#include "funddata.h"

FundData::FundData(QObject *parent) : QObject(parent)
{
    valuationsSpider = new FundDBSpider();
    singleSpider = new TiantianSpider();
    clearSingleData();

    QObject::connect(singleSpider, SIGNAL(finished(bool)), this, SLOT(gottenSingle(bool)));
    QObject::connect(valuationsSpider, SIGNAL(finished(bool)), this, SLOT(gottenValuations(bool)));
}

FundData::~FundData() {
    delete valuationsSpider;
    delete singleSpider;
}

void FundData::clearSingleData() {
    singleData.code = "NULL";
    singleData.name = "NULL";
    singleData.rate = 0;
    singleData.valuation = 0;
    singleData.timestamp = 0;
    singleData.stringData = "NULL";
}

void FundData::askSingle(QString code) {
    clearSingleData();
    singleData.code = code;
    singleSpider->ask(code);
}

void FundData::askValuations(QString code) {
    singleData.code = code;
    valuationsSpider->ask(code);
}

void FundData::gottenSingle(bool rslt) {
    if(rslt) {
        singleData.code = singleSpider->getList().at(0);
        singleData.name = singleSpider->getList().at(1);
        QString estimatedValue = singleSpider->getList().at(4);
        QString estimatedPersent = singleSpider->getList().at(5);
        QString estimatedTime = singleSpider->getList().at(6);
        singleData.rate = estimatedPersent.toFloat();
        singleData.valuation = estimatedValue.toFloat();
        singleData.timestamp = singleSpider->getList().at(7).toInt();
        if(singleData.name == "NULL") {
            singleData.stringData = singleData.code + " : " + estimatedValue + " (" + estimatedPersent + "%)" + "-" + estimatedTime;
        } else {
            singleData.stringData = singleData.name + " : " + estimatedValue + " (" + estimatedPersent + "%)" + "-" + estimatedTime;
        }
    }
    emit singleFinished(singleData.code, rslt);
}

void FundData::gottenValuations(bool rslt) {
    emit valuationsFinished(singleData.code, rslt);
}

funData_t FundData::getSingle() {
    return singleData;
}

QList<valuationData_t> FundData::getValuations() {
    return valuationsSpider->getList();
}

